﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using xmlclass;

namespace makefilexml
{
    public partial class mainform : Form
    {

        public class NodeInfo
        {
            public string filename { get; set; }
            public int version { get; set; }
            public string path { get; set; }
            public int fsize { get; set; }
        }

        public string currentdir = Environment.CurrentDirectory + "\\Debug";
        public XMLoperationClass xc;

        public List<NodeInfo> basefls;

        public mainform()
        {
            InitializeComponent();
        }


        private void AddFilesToLv(DirectoryInfo di)
        {
            FileInfo[] fls = di.GetFiles();
            int idx = 0;
            string tmp = string.Empty;
            foreach (FileInfo fi in fls)
            {
                ListViewItem lvi = listView1.Items.Add(fi.Name);//文件名
                int v = findFileVersion(fi.Name);
                if (v != -1)
                {
                    lvi.SubItems.Add((v + 1).ToString());//版本号
                }
                else
                {
                    lvi.SubItems.Add("1");//版本号
                }
                lvi.SubItems.Add(fi.Length.ToString());//文件长度
                idx = fi.DirectoryName.IndexOf(currentdir);//文件相对路径
                if (idx == -1)
                {
                    lvi.SubItems.Add("\\");
                }
                else
                {
                    tmp = fi.DirectoryName.Substring(idx + currentdir.Length);
                    if (tmp == string.Empty)
                    {
                        lvi.SubItems.Add("\\");
                    }
                    else
                    {
                        lvi.SubItems.Add(tmp);
                    }

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(currentdir);
            listView1.Items.Clear();
            AddFilesToLv(di);
            if (checkBox1.Checked)
            {
                AddDir(di);
            }
        }

        private void AddDir(DirectoryInfo d)
        {
            foreach (DirectoryInfo di in d.GetDirectories())
            {
                AddFilesToLv(di);
                AddDir(di);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            xc.CreateRootNode("filelist", false);
            xc.CreateNodeValue("updateurl", "url",textBox1.Text,true,false);
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                xc.CreateNodeValue("file", "filename", listView1.Items[i].Text, true, false);
                xc.SetAttribute("file", "filename", listView1.Items[i].Text, "version", listView1.Items[i].SubItems[1].Text);
                xc.SetAttribute("file", "filename", listView1.Items[i].Text, "filesize", listView1.Items[i].SubItems[2].Text);
                xc.SetAttribute("file", "filename", listView1.Items[i].Text, "path", listView1.Items[i].SubItems[3].Text);
            }
            if (xc.SaveToFile())
            {
                if (checkBox2.Checked)
                {
                    xc.SaveToFile("base.xml");
                }
                else
                {
                    ModifyBaseXML();
                }
                MessageBox.Show("save ok");
            }
            else
            {
                MessageBox.Show("save fail");
            }
        }

        public int findFileVersion(string filename)
        {
            foreach (NodeInfo ni in basefls)
            {
                if (ni.filename == filename)
                {
                    return ni.version;
                }
            }
            return -1;
        }
        private void Loadxmlfile(bool isShow)
        {
            if (File.Exists("base.xml"))
            {
                XMLoperationClass xcc = new XMLoperationClass("base.xml");
                xcc.GetRoot();
                List<string> fls = xcc.GetAttributeValues("file");
                if (fls.Count > 0)
                {
                    string str = "";
                    string[] p1;
                    for (int i = 0; i < fls.Count; i++)
                    {
                        str = fls[i];
                        p1 = str.Split('|');
                        if (p1.Length > 3)
                        {
                            NodeInfo ni = new NodeInfo();
                            ni.filename = p1[0].Substring(p1[0].IndexOf('=') + 1);
                            ni.version = Convert.ToInt32(p1[1].Substring(p1[1].IndexOf('=') + 1));
                            ni.fsize = Convert.ToInt32(p1[2].Substring(p1[2].IndexOf('=') + 1));
                            ni.path = p1[3].Substring(p1[3].IndexOf('=') + 1);
                            basefls.Add(ni);
                            if(isShow)
                            {
                                ListViewItem lvi = listView1.Items.Add(p1[0].Substring(p1[0].IndexOf('=') + 1));
                                lvi.SubItems.Add(p1[1].Substring(p1[1].IndexOf('=') + 1));
                                lvi.SubItems.Add(p1[2].Substring(p1[2].IndexOf('=') + 1));
                                lvi.SubItems.Add(p1[3].Substring(p1[3].IndexOf('=') + 1));
                            }
                        }
                    }
                }
            }
        }

        private void ModifyBaseXML()
        {
            if (File.Exists("base.xml"))
            {
                XMLoperationClass xcc = new XMLoperationClass("base.xml");
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    xcc.SetAttribute("file", "filename", listView1.Items[i].Text, "version", listView1.Items[i].SubItems[1].Text);
                    xcc.SetAttribute("file", "filename", listView1.Items[i].Text, "filesize", listView1.Items[i].SubItems[2].Text);
                    xcc.SetAttribute("file", "filename", listView1.Items[i].Text, "path", listView1.Items[i].SubItems[3].Text);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            basefls = new List<NodeInfo>();
            xc = new XMLoperationClass();
            xc.filename = "fileversion.xml";
            xc.GetRoot();
            Loadxmlfile(false);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            Loadxmlfile(true);
        }
    }
}
